package com.gainloop.Listeners;

import java.util.ArrayList;
import java.util.LinkedList;

import org.alexd.jsonrpc.JSONRPCException;

import com.gainloop.R;
import com.gainloop.Api.ApiResponseObject;
import com.gainloop.Api.Wishes;
import com.gainloop.Api.Entity.Tag;
import com.gainloop.Api.Entity.Wish;
import com.gainloop.Data.Registry;
import com.gainloop.Utils.Constants;
import com.gainloop.Utils.ConvertUtils;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class submitNewWishListener implements View.OnClickListener{
	
	static final int MIN_DESC_LENGTH = 5 ;
	static final int MAX_DESC_LENGTH = 150 ;
	
	private Context _context;
	private EditText _gainDesc;
	private Intent _intent;
	private LinkedList<String> _globalTagList;
	
	public submitNewWishListener(EditText gainDesc,Intent intent, Context context, LinkedList<String> addedTags){
		_context = context;
		_gainDesc = gainDesc;
		_intent = intent;
		_globalTagList = addedTags;
	}
	
	@Override
	public void onClick(View arg0) {
		Wish newWish = new Wish();
		
		Long _curCategoryId = _intent.getExtras().getLong(Constants.CATEGORY_ID);
		if(_curCategoryId!=null){
			newWish.setCategoryId(_curCategoryId); 
			Log.v("cur Category Id=",Long.toString(_curCategoryId));
		}
		else{
			Log.v("cur Category Id=","null");
			throw new RuntimeException();
		}
		
    	String gainDescVal = _gainDesc.getText().toString();
    	if(gainDescVal.length()<MIN_DESC_LENGTH){
    		Toast toast = Toast.makeText(_context, "Please enter an informative description.", Toast.LENGTH_LONG);
			toast.show();
    		return;
    	}
    	if(gainDescVal.length()>MAX_DESC_LENGTH){
    		Toast toast = Toast.makeText(_context, "Wish description to long!!!", Toast.LENGTH_LONG);
			toast.show();
    		return;
    	}

    	Log.v("gainDescVal length=",Integer.toString(gainDescVal.length()));
    	
    	newWish.setContent(_gainDesc.getText().toString());
    	
    	
    	newWish.setUserId(Registry.GetUser().getId());
    	
    	Log.v("gain desc=",newWish.getContent());
    	Log.v("gain status=",newWish.getStatus().toString());
    	Log.v("gain user Id=",Long.toString(newWish.getUserId()));
    	Log.v("gain Category Id=",Long.toString(newWish.getCategoryId()));
    	
    	ArrayList<Tag> tagList = ConvertUtils.transformToTagList(_globalTagList);
    	
    	Log.v("tagList size=",Integer.toString(tagList.size()));
    	
    	Wishes wishes = Wishes.getInstance();            
    	ApiResponseObject<Wish> resp = null;
    	try {           
			resp = wishes.create(newWish,tagList);
		} catch (JSONRPCException e) {
			declareInternalError();
			Log.v("create gain","Failed");
			e.printStackTrace();
			return;
		}
		int resCode = resp.getReturnCode(); 
		
		Log.v("getReturnCode=",Integer.toString(resCode) + " Error: " + resp.getError());
		
		if(resCode==0){
			Toast toast = Toast.makeText(_context, R.string.gain_added, Toast.LENGTH_SHORT);
			toast.show();
			
			
			//clean the fields.
			_globalTagList.clear();
			_gainDesc.setText("");
			
			//go to the new wish page.?? - do we want this??? - Meir		
			//Long newWishId = resp.getData().getId();
		}
		else{
			declareInternalError();
			Log.v("create gain","Failed");
		}
	}

	private void declareInternalError() {
		Toast toast = Toast.makeText(_context, R.string.internal_error, Toast.LENGTH_LONG);
		toast.show();
		
	}

}
